<script setup lang="ts">
import type {FormInstance, FormRules} from 'element-plus'

const ruleFormRef = ref<FormInstance>()

const checkPhone = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error('Please input the phone'))
  }
  if (!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(value)) {
    return callback(new Error('手机格式错误'))
  }
  callback()
}

const validateUsername = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the username'))
  }
  if (!/^[A-Za-z0-9]+$/.test(value)) {
    callback(new Error('用户名只能由数字和字母组成'))
  }
  callback()
}
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the password'))
  }
  if (!/^[A-Za-z0-9]+$/.test(value)) {
    callback(new Error('密码只允许数字和字母的组合'))
  }
  callback()
}

const ruleForm = reactive({
  username: '',
  phone: '',
  password: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
  username: [{validator: validateUsername, trigger: 'blur'}],
  phone: [{validator: checkPhone, trigger: 'blur'}],
  password: [{validator: validatePassword, trigger: 'blur'}],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      cFetch('/account/add',{
        auth:true,
        defaultOpts:{
          method:"POST",
          body:{
            username:ruleForm.username,
            phone:ruleForm.phone,
            password:ruleForm.password
          }
        }
      }).then(res=>{
        if(res.code===200){
          ElMessage.success('添加成功')
          resetForm(ruleFormRef.value)
        }
      })
      // console.log('submit!')
    } else {
      console.log('error submit!')
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<template>
  <div class="content">
    <h3>新增用户</h3>
    <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
            v-model.trim.lazy="ruleForm.username"
            autocomplete="off"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="phone">
        <el-input
            v-model.trim.lazy="ruleForm.phone"
            autocomplete="off"
        />
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
            v-model.number="ruleForm.password"
            autocomplete="off"
        />
      </el-form-item>

      <div class="btn-area">
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          确认新增
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">重置表单</el-button>
      </div>
    </el-form>
  </div>
</template>

<style scoped lang="scss">
.content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.btn-area {
  display: flex;
  justify-content: space-evenly;
}
</style>